package com.bjgov.scoresys.action;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.MappingDispatchAction;

import com.bjgov.scoresys.entity.ScoreTree;
import com.bjgov.scoresys.form.ScoreFrameForm;
import com.bjgov.scoresys.service.ScoreTreeHandler;

public class ScoreFrameAction extends MappingDispatchAction{
	
	/**
	 * 添加框架，需要检测重复
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public ActionForward addFrame(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)throws Exception
	{
		//ScoreFrameForm frameForm = new ScoreFrameForm();
		
		ScoreFrameForm frameForm = (ScoreFrameForm)form;
		
		if(frameForm.getCateName().length()==0)
		{
			request.setAttribute("message", "评价项不能为空！");
			request.setAttribute("returnURL", "/system/addScoreFrame.jsp");
			return mapping.findForward("fail");
		}
		
		ScoreTree tree = new ScoreTree();
		
		int maxID = ScoreTreeHandler.getMaxID();
		String deep_path ="";
		if(maxID==0)
		{
			deep_path="1";//第一次添加
		}
		else
		{
			if(frameForm.getPid()==0)//做为顶级
			{
				deep_path=(maxID+1)+"";
			}
			else
			{
				deep_path= ScoreTreeHandler.loadById(frameForm.getPid()).getDeepPath()+","+(maxID+1);
			}
		}
		
		tree.setId(maxID+1);
		tree.setCateName(frameForm.getCateName());
		tree.setPid(frameForm.getPid());
		tree.setDeepPath(deep_path);
		tree.setIsControl(frameForm.getIsControl());
		tree.setHasScoreItem(frameForm.getHasScoreItem());
		tree.setStatus((short)0);
		tree.setPercent(frameForm.getPercent());
		ScoreTreeHandler.saveFrame(tree);
		
		
		request.setAttribute("message", "添加成功！");
		request.setAttribute("nextURL", mapping.getInput());
		return mapping.findForward("tip");
	}
	
	/**
	 * 修改评分项,先不考虑重复
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public ActionForward editFrame(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)throws Exception
	{
		//ScoreFrameForm frameForm = new ScoreFrameForm();
		
		ScoreFrameForm frameForm = (ScoreFrameForm)form;
		
		if(frameForm.getCateName().length()==0)
		{
			request.setAttribute("message", "评价项名称不能为空！");
			request.setAttribute("returnURL", "/system/editScoreFrame.jsp");
			return mapping.findForward("fail");
		}
		
		ScoreTree tree_original= ScoreTreeHandler.loadById(frameForm.getId());//原对像
		
		int pid = frameForm.getPid();
		String deep_path =tree_original.getDeepPath();
		if(pid!=0)
		{
			if(pid==tree_original.getId())
			{
				deep_path = tree_original.getDeepPath();
				pid = tree_original.getPid();
			}
			else
			{
				deep_path=ScoreTreeHandler.loadById(frameForm.getPid()).getDeepPath()+","+frameForm.getId();
			}
		}
		else
		{
			deep_path = frameForm.getId()+"";//原对像目录
		}
		
		tree_original.setCateName(frameForm.getCateName());
		tree_original.setPid(pid);
		tree_original.setDeepPath(deep_path);
		tree_original.setIsControl(frameForm.getIsControl());
		tree_original.setHasScoreItem(frameForm.getHasScoreItem());
		tree_original.setStatus((short)0);
		tree_original.setPercent(frameForm.getPercent());
		ScoreTreeHandler.saveFrame(tree_original);
		
		
		request.setAttribute("message", "修改成功！");
		request.setAttribute("nextURL", "manageScoreFrame.jsp");
		return mapping.findForward("tip");
	}

}
